'use strict';

angular.module('myApp.tasks', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/tasks/:status', {
        templateUrl: '../views/tasks.html',
        controller: 'tasksCtrl'
    });
}])

.controller('tasksCtrl', [
    '$scope',
    '$route',
    '$routeParams',
    '$location',
    function($scope, $route, $routeParams, $location) {
        let status = $routeParams.status;
        $scope.tasks = [{
            id: 1,
            text: '上报计划',
            complete: false
        }, {
            id: 2,
            text: '维护系统',
            complete: false
        }, {
            id: 3,
            text: '物流报表',
            complete: true
        }, ];
        $scope.text = '';
        $scope.ok = false;
        $scope.filterTask = '';
        $scope.addTask = function() {
            $scope.tasks.push({
                id: Math.ceil(Math.random() * 1000),
                text: $scope.text,
                complete: false
            })
            $scope.text = '';
        };
        $scope.del = function(tt) {
            let index = $scope.tasks.indexOf(tt);
            $scope.tasks.splice(index, 1);
        };
        $scope.checkAll = function(ok) {
            $scope.tasks.map(task => {
                task.complete = ok
            });
        };
        // $scope.filteryn = { ok: false };
        $scope.filteryn = {};
        $scope.$location = $location;
        $scope.$watch('$location.path()', (now, old) => {
            switch (now) {
                case '/tasks/no':
                    $scope.filteryn.ok = false;
                    break;
                case '/tasks/yes':
                    $scope.filteryn.ok = true;
                    break;
                default:
                    // $scope.filteryn = {}
                    break;
            }
        })
    }
]);